﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using élénkPróbája.extentions;
using élénkPróbája.Test.dto;

namespace élénkPróbája.Test
{
    [TestClass]
    public class ValidateAndThrowTester
    {
        public ValidateAndThrowTester()
        {
            CultureScope.SetDefaultCulture();
        }

        [TestMethod]
        public void Throws_exception()
        {
            var validator = new TestValidator {
                v => v.RuleFor(x => x.PublicName).NotNull()
            }; 
            MyAssert.Throws<ValidationException>(() => validator.ValidateAndThrow(new Company()));
        }

        [TestMethod]
        public void Throws_exception_async()
        {
            var validator = new TestValidator {
                v => v.RuleFor(x => x.PublicName).NotNull()
            };

            MyAssert.Throws<ValidationException>(() => {
                try
                {
                    validator.ValidateAndThrowAsync(new Company()).Wait();
                }
                catch (AggregateException agrEx)
                {
                    throw agrEx.InnerException;
                }
            });
        }

        [TestMethod]
        public void Does_not_throw_when_valid()
        {
            var validator = new TestValidator {
                v => v.RuleFor(x => x.PublicName).NotNull()
            };

            validator.ValidateAndThrow(new Company { PublicName = "foo" });
        }

        public void Does_not_throw_when_valid_async()
        {
            var validator = new TestValidator {
                v => v.RuleFor(x => x.PublicName).NotNull()
            };

            validator.ValidateAndThrowAsync(new Company { PublicName = "foo" }).Wait();
        }
    }
}